// pages/citySearch/cityRank/cityRank.js
import { scrollToLeftEnd } from '../../../utils/behavior';
const app = getApp();
Page({
    behaviors: [scrollToLeftEnd],
    /**
     * 页面的初始数据
     */
    data: {
        tabsArr: ['国内', '国际/港澳台'],
        currentTabIndex: 0,
        // 轮播窗口高度
        swiperHeight: '',
        // 城市列表数据类型 datapoint录入数据点  specialpoint网络特殊点  checkin测试打卡数 默认打卡数
        sortRulesArr: [
            {
                cityListType: 'checkin',
                cityListTypeText: '测试打卡数',
            },
            {
                cityListType: 'datapoint',
                cityListTypeText: '录入数据点',
            },
            {
                cityListType: 'specialpoint',
                cityListTypeText: '网络特殊点',
            },
        ],
        // 当前排序规则索引
        currentSortRuleIndex: 0,
        // 当前排序规则索引海外
        currentSortRuleIndexOversea: 0,
        // 有记录的城市数组
        recordCityArr: [],
        recordCityArrOversea: [],
        loadingOver: false,
        loadingOverOversea: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCityRankList(
            '00',
            this.data.currentSortRuleIndex,
            'recordCityArr'
        );
        this.getCityRankList(
            '01',
            this.data.currentSortRuleIndexOversea,
            'recordCityArrOversea'
        );
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                let clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth,
                    swiperHeight;
                swiperHeight = clientHeight * rpxR - 80;
                that.setData({
                    swiperHeight: swiperHeight,
                });
            },
        });
        this.setData({
            currentTabIndex: options.oversea == '01' ? 1 : 0,
        });
    },

    onTabsClick(e) {
        this.setData({
            currentTabIndex: e.detail.index,
        });
    },

    swiperChange(e) {
        this.setData({
            currentTabIndex: e.detail.current,
        });
    },

    // 改变排序规则
    changeSortRule(e) {
        let { oversea, index } = e.currentTarget.dataset;
        if (oversea) {
            this.setData({
                currentSortRuleIndexOversea: index,
            });
            this.getCityRankList('01', index, 'recordCityArrOversea');
        } else {
            this.setData({
                currentSortRuleIndex: index,
            });
            this.getCityRankList('00', index, 'recordCityArr');
        }
    },

    // 获取城市排行榜
    getCityRankList(type, index, arrName) {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + 'cjMap/getCityInitialRankList',
            data: {
                authId: app.globalData.authId,
                oversea: type,
                listtype: _that.data.sortRulesArr[index].cityListType,
            },
            success(res) {
                if (res.data.status === '0') {
                    // 定义一个暂时的空数组
                    let tempRecordCity = [];
                    tempRecordCity = res.data.data;
                    tempRecordCity.forEach((item) => {
                        if (item.enname) {
                            item.foreignName =
                                item.enname &&
                                (item.enname.split(',')[2] ||
                                    item.enname.split(',')[1]);
                        }
                    });
                    _that.setData({
                        [arrName]: tempRecordCity,
                    });
                    if (type === '00') {
                        _that.setData({
                            loadingOver: true,
                        });
                    } else if (type === '01') {
                        _that.setData({
                            loadingOverOversea: true,
                        });
                    }
                }
            },
        });
    },

    goToIndex(e) {
        let _that = this;

        let country = e.currentTarget.dataset.country;
        let city = e.currentTarget.dataset.city;

        let lng = e.currentTarget.dataset.lng;
        let lat = e.currentTarget.dataset.lat;
        // 有些城市后台暂时没有经纬度
        if (lng && lat) {
            // 修改全局变量
            app.globalData.countryBack = country;
            app.globalData.addressBack = city;

            // 创建历史记录
            _that.createCitySearchHistory(app.globalData.authId, city, country);

            let pages = getCurrentPages(); //当前页面栈
            let prevPage = pages[pages.length - 3]; //上上一页面（地图首页）
            prevPage.setData({
                //直接给index页面赋值
                mapLongitude: lng,
                mapLatitude: lat,
                mapScale: 12,
            });
            wx.switchTab({
                url: '../../index/index',
            });
        } else {
            wx.showModal({
                title: '位置获取失败',
                content: '当前城市暂未覆盖，请联系后台管理员',
                showCancel: false,
            });
        }
    },

    // 创建历史记录
    createCitySearchHistory(authId, city, country) {
        wx.request({
            url: app.globalData.rootPath + 'cjMap/createCitySearchHistory',
            data: {
                authId,
                city,
                country,
            },
            success(res) {
                console.log(res);
            },
        });
    },
});
